
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/15/2011 23:05:24
-- Generated from EDMX file: C:\Projects\Telerik Academy\netessentials\NetDatabaseAndXmlSample\CompanyDatabase\CompanyDatabase.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [CompanyDatabase];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CompanyCountry]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CompanySet] DROP CONSTRAINT [FK_CompanyCountry];
GO
IF OBJECT_ID(N'[dbo].[FK_Customer_inherits_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Customer] DROP CONSTRAINT [FK_Customer_inherits_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomerCompany_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CustomerCompany] DROP CONSTRAINT [FK_CustomerCompany_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomerCompany_Customer]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CustomerCompany] DROP CONSTRAINT [FK_CustomerCompany_Customer];
GO
IF OBJECT_ID(N'[dbo].[FK_Employee_inherits_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Employee] DROP CONSTRAINT [FK_Employee_inherits_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_EmployeeCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Employee] DROP CONSTRAINT [FK_EmployeeCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonCountry]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet] DROP CONSTRAINT [FK_PersonCountry];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[CompanySet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CompanySet];
GO
IF OBJECT_ID(N'[dbo].[CountrySet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CountrySet];
GO
IF OBJECT_ID(N'[dbo].[CustomerCompany]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CustomerCompany];
GO
IF OBJECT_ID(N'[dbo].[PersonSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet];
GO
IF OBJECT_ID(N'[dbo].[PersonSet_Customer]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet_Customer];
GO
IF OBJECT_ID(N'[dbo].[PersonSet_Employee]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet_Employee];
GO
IF OBJECT_ID(N'[CompanyDatabaseStoreContainer].[CustomersPerCountry]', 'U') IS NOT NULL
    DROP TABLE [CompanyDatabaseStoreContainer].[CustomersPerCountry];
GO
IF OBJECT_ID(N'[CompanyDatabaseStoreContainer].[EmployeesPerCountry]', 'U') IS NOT NULL
    DROP TABLE [CompanyDatabaseStoreContainer].[EmployeesPerCountry];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'CompanySet'
CREATE TABLE [dbo].[CompanySet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(30)  NOT NULL,
    [Industry] nvarchar(50)  NOT NULL,
    [CountryId] int  NOT NULL
);
GO

-- Creating table 'PersonSet'
CREATE TABLE [dbo].[PersonSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [FirstName] nvarchar(30)  NOT NULL,
    [LastName] nvarchar(30)  NOT NULL,
    [CountryId] int  NOT NULL,
    [Telephone] nvarchar(30)  NOT NULL
);
GO

-- Creating table 'CountrySet'
CREATE TABLE [dbo].[CountrySet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(50)  NOT NULL,
    [NativeLanguage] nvarchar(30)  NOT NULL
);
GO

-- Creating table 'CustomersPerCountries'
CREATE TABLE [dbo].[CustomersPerCountries] (
    [Country] nvarchar(50)  NOT NULL,
    [Count] int  NULL
);
GO

-- Creating table 'EmployeesPerCountries'
CREATE TABLE [dbo].[EmployeesPerCountries] (
    [Country] nvarchar(50)  NOT NULL,
    [Count] int  NULL
);
GO

-- Creating table 'PersonSet_Employee'
CREATE TABLE [dbo].[PersonSet_Employee] (
    [CompanyId] int  NOT NULL,
    [HideDate] datetime  NOT NULL,
    [Salary] decimal(18,0)  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'PersonSet_Customer'
CREATE TABLE [dbo].[PersonSet_Customer] (
    [Id] int  NOT NULL
);
GO

-- Creating table 'CustomerCompany'
CREATE TABLE [dbo].[CustomerCompany] (
    [Customers_Id] int  NOT NULL,
    [Companies_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'CompanySet'
ALTER TABLE [dbo].[CompanySet]
ADD CONSTRAINT [PK_CompanySet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [PK_PersonSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CountrySet'
ALTER TABLE [dbo].[CountrySet]
ADD CONSTRAINT [PK_CountrySet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Country] in table 'CustomersPerCountries'
ALTER TABLE [dbo].[CustomersPerCountries]
ADD CONSTRAINT [PK_CustomersPerCountries]
    PRIMARY KEY CLUSTERED ([Country] ASC);
GO

-- Creating primary key on [Country] in table 'EmployeesPerCountries'
ALTER TABLE [dbo].[EmployeesPerCountries]
ADD CONSTRAINT [PK_EmployeesPerCountries]
    PRIMARY KEY CLUSTERED ([Country] ASC);
GO

-- Creating primary key on [Id] in table 'PersonSet_Employee'
ALTER TABLE [dbo].[PersonSet_Employee]
ADD CONSTRAINT [PK_PersonSet_Employee]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PersonSet_Customer'
ALTER TABLE [dbo].[PersonSet_Customer]
ADD CONSTRAINT [PK_PersonSet_Customer]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Customers_Id], [Companies_Id] in table 'CustomerCompany'
ALTER TABLE [dbo].[CustomerCompany]
ADD CONSTRAINT [PK_CustomerCompany]
    PRIMARY KEY NONCLUSTERED ([Customers_Id], [Companies_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [CountryId] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [FK_PersonCountry]
    FOREIGN KEY ([CountryId])
    REFERENCES [dbo].[CountrySet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonCountry'
CREATE INDEX [IX_FK_PersonCountry]
ON [dbo].[PersonSet]
    ([CountryId]);
GO

-- Creating foreign key on [CountryId] in table 'CompanySet'
ALTER TABLE [dbo].[CompanySet]
ADD CONSTRAINT [FK_CompanyCountry]
    FOREIGN KEY ([CountryId])
    REFERENCES [dbo].[CountrySet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyCountry'
CREATE INDEX [IX_FK_CompanyCountry]
ON [dbo].[CompanySet]
    ([CountryId]);
GO

-- Creating foreign key on [CompanyId] in table 'PersonSet_Employee'
ALTER TABLE [dbo].[PersonSet_Employee]
ADD CONSTRAINT [FK_EmployeeCompany]
    FOREIGN KEY ([CompanyId])
    REFERENCES [dbo].[CompanySet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EmployeeCompany'
CREATE INDEX [IX_FK_EmployeeCompany]
ON [dbo].[PersonSet_Employee]
    ([CompanyId]);
GO

-- Creating foreign key on [Customers_Id] in table 'CustomerCompany'
ALTER TABLE [dbo].[CustomerCompany]
ADD CONSTRAINT [FK_CustomerCompany_Customer]
    FOREIGN KEY ([Customers_Id])
    REFERENCES [dbo].[PersonSet_Customer]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Companies_Id] in table 'CustomerCompany'
ALTER TABLE [dbo].[CustomerCompany]
ADD CONSTRAINT [FK_CustomerCompany_Company]
    FOREIGN KEY ([Companies_Id])
    REFERENCES [dbo].[CompanySet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomerCompany_Company'
CREATE INDEX [IX_FK_CustomerCompany_Company]
ON [dbo].[CustomerCompany]
    ([Companies_Id]);
GO

-- Creating foreign key on [Id] in table 'PersonSet_Employee'
ALTER TABLE [dbo].[PersonSet_Employee]
ADD CONSTRAINT [FK_Employee_inherits_Person]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[PersonSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'PersonSet_Customer'
ALTER TABLE [dbo].[PersonSet_Customer]
ADD CONSTRAINT [FK_Customer_inherits_Person]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[PersonSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------